﻿namespace Cubezicles.UI
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    public class ManagerPane : DrawableGameComponent
    {
        // Get the Game1 object stored by the base class
        protected Game1 game { get { return (Game1)Game; } }

        // Is the window currently showing/visible?
        public bool IsVisible { get; set; }

        public void ToggleVisible()
        {
            IsVisible = !IsVisible;
        }

        // Is the game paused while this pane is displaying?
        public bool PausedWhileOpen { get; protected set; }

        // Pane/frame position
        public Vector2 PanePosition { get; protected set; }

        // Pane/frame size
        // w = x h = y
        public Vector2 PaneSize { get; protected set; }

        // Title that displays in the title bar of the frame
        public String FrameTitle { get; set; }

        public SpriteFont FrameTitleFont { get; set; }

        // Textures
        internal Texture2D butts;

        protected List<Button> controls;

        public ManagerPane(Game1 game) : base(game)
        {

            this.DrawOrder = (int)Game1.DisplayOrder.UI;

            PanePosition = new Vector2(0, 0);//(xPos, yPos);
            PaneSize = new Vector2(0,0); //(w, h);
            FrameTitle = ""; //title;

            IsVisible = false;

            PausedWhileOpen = false; // only true for money manager end of day report as of now
        }

        protected override void LoadContent()
        {
            butts = Game.Content.Load<Texture2D>("UI/buttons");

            base.LoadContent();
        }

        /// <summary>
        /// Check if a mouse click was inside a showing manager pane
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool IsInsidePane(int x, int y)
        {
            // If this pane isn't showing, the click couldn't have been inside it
            if (!IsVisible) return false;

            Rectangle bound = new Rectangle((int)PanePosition.X, (int)PanePosition.Y, (int)PaneSize.X, (int)PaneSize.Y);
            if (bound.Contains(x, y)) return true;

            return false;
        }

        protected void DrawFrame()
        {
            // alt to game.drawFrame
            game.drawFrame((int)PanePosition.X, (int)PanePosition.Y, (int)PaneSize.X, (int)PaneSize.Y, FrameTitle, game.Fonts["status"]);
        }

        public override void Update(GameTime gameTime)
        {
            // close window if press enter
            if (game.oldks.IsKeyDown(Keys.Enter) && game.ks.IsKeyUp(Keys.Enter))
            {
                if (IsVisible) ClosePane();
            }
                
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (IsVisible)
            {
                // Create a list of UI controls
                controls = new List<Button>();

                // Get the screen width and height
                //int screenH = GraphicsDevice.Viewport.Height;
                //int screenW = GraphicsDevice.Viewport.Width;

                // Pane position
                int x = (int)PanePosition.X;
                int y = (int)PanePosition.Y;

                // Pane size
                int w = (int) PaneSize.X;
                int h = (int) PaneSize.Y;
                            
                SpriteFont titleFont = game.Fonts["status"];

                game.SpriteBatch.Begin();

                // Draw the frame that holds the info
                game.drawFrame(x, y, w, h, FrameTitle, titleFont); 
               

                // Draw the okay button
                // DrawOKbutton(x, y); 

                // Draw ALL the buttons
                controls.ForEach(control => control.Update(GraphicsDevice.Viewport, game.ms, game.oldms));
                controls.ForEach(control => control.Draw(game.SpriteBatch, GraphicsDevice.Viewport));

                game.SpriteBatch.End();
        
             }
        }

        internal void DrawOKbutton(int x, int y)
        {
            // Draw the okay button
            Button okButton = new Button()
            //controls.Add(new Button()
            {
                Texture = butts,
                font = game.Fonts["info"],
                text = "Okay",
                xpos = x,
                ypos = y,
                Active = true,
                OnClick = (cont) => ClosePane()
            };
            okButton.Enable();
            controls.Add(okButton);
        }
       
        // able to be overridden in case need more functionality
        public virtual void ClosePane()
        {
            // typically called through okay button
            // usually just hides window
            // but might have additional functionality, like showing another pane or unpausing

            IsVisible = false;
            if (game.time.CurrTimeSpeed == Clock.TimeSpeed.Paused)
                game.time.PauseUnpause();
        }
    }
}

    

   